package com.zm.shop.users.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zm.shop.users.entity.UsersEntity;
import com.zm.shop.users.service.UsersService;
import com.zm.shop.common.utils.PageUtils;
import com.zm.shop.common.utils.R;



/**
 * 
 *
 * @author Java2103
 * @email Java2103@qq.com
 * @date 2021-08-03 17:23:22
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;



    /**
     * 通过字段（username  email）获取用户
     */
    @PostMapping("/findUser")
    public UsersEntity findUser(String field, String value) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(field, value);
        return usersService.getOne(wrapper);
    }

    @GetMapping("/queryUser")
    public UsersEntity queryUser(String name){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",name);
        return usersService.getOne(queryWrapper);

    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = usersService.queryPage(params);
        Integer totalCount=page.getList().size();
        page.setTotalCount(totalCount);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UsersEntity users = usersService.getById(id);

        return R.ok().put("users", users);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UsersEntity users){
		usersService.save(users);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UsersEntity users){
		usersService.updateById(users);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		usersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
